/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


package com.newgonzo.cannonball.dom.traversal
{	
	import com.newgonzo.cannonball.w3c.dom.traversal.ITreeWalker;
	import com.newgonzo.cannonball.w3c.dom.traversal.INodeFilter;
	import com.newgonzo.cannonball.w3c.dom.traversal.NodeFilter;
	import com.newgonzo.cannonball.w3c.dom.IDocument;
	import com.newgonzo.cannonball.w3c.dom.INode;
	
	public class TreeWalker implements ITreeWalker
	{
		private var nodeFilter:INodeFilter;
		
		private var childIndex:int = -1;
		private var root:INode;
		private var show:int;
		private var current:INode;
		
		public function TreeWalker(rootNode:INode, whatToShow:int = -1, nodeFilter:INodeFilter = null)
		{
			this.root = rootNode;
			this.show = whatToShow;
			this.current = rootNode;
			this.nodeFilter = nodeFilter;
		}
		
		public function get rootNode():INode
		{
			return this.root;
		}
		
		public function get whatToShow():int
		{
			return show;
		}
		
		public function get currentNode():INode
		{
			return current as INode;
		}
		
		public function set currentNode(value:INode):void
		{
			current = value;
		}
		
		public function get filter():INodeFilter
		{
			return nodeFilter;
		}
		
		public function nextNode():INode
		{
			var result:INode = getFirstChild(current);
			
			if(result)
			{
				current = result;
				return current;
			}
			
			result = getNextSibling(current, root);
			
			if(result)
			{
				current = result;
				return current;
			}
			
			var parent:INode = current;
			
			while(true)
			{
				parent = getParentNode(parent);
				
				if(parent == null)
				{
					return null;
				}
				
				result = getNextSibling(parent, root);
				
				if(result)
				{
					current = result;
					return current;
				}
			}
			
			return null;
		}
		
		public function previousNode():INode
		{
			var result:INode = getPreviousSibling(current, root);
			
			if(result == null)
			{
				result = getParentNode(current);
				
				if(result != null)
				{
					current = result;
				}
				
				return result;
			}
			
			var n:INode = getLastChild(result);
			var last:INode = n;
			
			while(n != null)
			{
				last = n;
				n = getLastChild(last);
			}
			
			current = last != null ? last : result;
			return current;
		}
		
		public function parentNode():INode
		{
			var newCurrent:INode = getParentNode(current);
			
			if(newCurrent)
			{
				current = newCurrent;
			}
			
			return newCurrent;
		}
		
		public function firstChild():INode
		{
			var newCurrent:INode = getFirstChild(current);
			
			if(newCurrent)
			{
				current = newCurrent;
			}
			
			return newCurrent;
		}
		
		public function lastChild():INode
		{
			var newCurrent:INode = getLastChild(current);
			
			if(newCurrent)
			{
				current = newCurrent;
			}
			
			return newCurrent;
		}
		
		public function nextSibling():INode
		{
			var newCurrent:INode = getNextSibling(current, root);
			
			if(newCurrent)
			{
				current = newCurrent;
			}
			
			return newCurrent;
		}
		
		public function previousSibling():INode
		{
			var newCurrent:INode = getPreviousSibling(current, root);
			
			if(newCurrent)
			{
				current = newCurrent;
			}
			
			return newCurrent;
		}
		
		
		protected function getFirstChild(node:INode):INode
		{
			var result:INode = node.firstChild;
			
			if(result == null)
			{
			    return null;
			}
			
			var accept:uint = acceptNode(result);
			
			switch(accept)
			{
				case NodeFilter.FILTER_ACCEPT:
					
					return result;
				
				case NodeFilter.FILTER_SKIP:
					
					var n:INode = getFirstChild(result);
					
					if(n != null)
					{
						return n;
					}
					
					// no break
					
				default: // NodeFilter.FILTER_REJECT
					return getNextSibling(result, node);		
			}
			
			return null;
		}
		
		protected function getLastChild(node:INode):INode
		{
			var result:INode = current.lastChild;
		
			if(result == null)
			{
				return null;
			}
			
			var accept:uint = acceptNode(result);
			
			switch(accept)
			{
				case NodeFilter.FILTER_ACCEPT:
					return result;
				
				case NodeFilter.FILTER_SKIP:
					
					var n:INode = getLastChild(result);
					
					if(n != null)
					{
						return n;
					}
					
					// no break
				default: // NodeFilter.FILTER_REJECT
					return getPreviousSibling(result, node);
			}
			
			return null;
		}
		
		protected function getNextSibling(node:INode, root:INode):INode
		{
			while(true)
			{
				if(node == root)
				{
					return null;
				}
				
				result = node.nextSibling;
				
				if(result == null)
				{
					var result:INode = node.parentNode;
					
					if(result == null || result == root)
					{
						return null;
					}
					
					if(acceptNode(result) == NodeFilter.FILTER_SKIP)
					{
						node = result;
						continue;
					}
					
					return null;
				}
				
				var accept:uint = acceptNode(result);
				
				switch(accept)
				{
					case NodeFilter.FILTER_ACCEPT:
						return result;
						
					case NodeFilter.FILTER_SKIP:
					
						var n:INode = getFirstChild(result);
						
						if(n != null)
						{
							return n;
						}
						
						// no break
						
					default: // NodeFilter.FILTER_REJECT
						node = result;
						continue;
				}
			}
			
			return null;
		}
		
		protected function getPreviousSibling(node:INode, root:INode):INode
		{
			while(true)
			{
				if(node == root)
				{
					return null;
				}
				
				var result:INode = node.previousSibling;
				
				if(result == null)
				{
					result = node.parentNode;
					
					if(result == null || result == root)
					{
						return null;
					}
					
					if(acceptNode(result) == NodeFilter.FILTER_SKIP)
					{
						node = result;
						continue;
					}
					
					return null;
				}
				
				var accept:uint = acceptNode(result);
				
				switch(accept)
				{
					case NodeFilter.FILTER_ACCEPT:
						return result;
						
					case NodeFilter.FILTER_SKIP:
					
						var n:INode = getLastChild(result);
						
						if(n != null)
						{
							return n;
						}
						
						// no break
						
					default: // NodeFilter.FILTER_REJECT
						node = result;
						continue;
				}
			}
			
			return null;
		}
		
		protected function getParentNode(node:INode):INode
		{
			if(node == root)
			{
				return null;
			}
			
			var result:INode = node;
			
			while(true)
			{
				result = result.parentNode;
				
				if(result == null)
				{
					return null;
				}
				
				if(nodeFilter == null || nodeFilter.acceptNode(result) == NodeFilter.FILTER_ACCEPT)
				{
					return result;
				}
			}
			
			return null;
		}
		
		protected function acceptNode(node:INode):uint
		{
			if(nodeFilter)
			{
				return nodeFilter.acceptNode(node);
			}
			
			return NodeFilter.FILTER_ACCEPT;
		}
	}
}